package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.enums.FeeBackEnum;
import com.settlement.system.common.enums.UserReadEnum;
import com.settlement.system.mapper.SysDemandMapper;
import com.settlement.system.mapper.SysLazyToolUserMapper;
import com.settlement.system.model.entity.SysDemand;
import com.settlement.system.model.entity.SysLazyToolUser;
import com.settlement.system.model.form.SysDemandForm;
import com.settlement.system.model.query.SysDemandQuery;
import com.settlement.system.model.vo.SysDemandImportVo;
import com.settlement.system.service.SysDemandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户需求表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-12-28
 */
@Service
@RequiredArgsConstructor
public class SysDemandServiceImpl extends ServiceImpl<SysDemandMapper, SysDemand> implements SysDemandService {

    private final SysLazyToolUserMapper sysLazyToolUserMapper;

    @Override
    public Page<SysDemand> getPage(SysDemandQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysDemand> page = new Page<>(pageNum, pageSize);
        Page<SysDemand> demandPage = this.baseMapper.getPage(page, queryParams);
        if(demandPage.getRecords().size() > 0){
            for (SysDemand record : demandPage.getRecords()) {
                if (FeeBackEnum.N.getValue().equals(record.getStatus())) {
                    record.setStatusName(FeeBackEnum.N.getLabel());
                } else {
                    record.setStatusName(FeeBackEnum.Y.getLabel());
                }
                if (UserReadEnum.N.getValue().equals(record.getUserRead())) {
                    record.setUserReadName(UserReadEnum.N.getLabel());
                } else {
                    record.setUserReadName(UserReadEnum.Y.getLabel());
                }
            }
        }
        return demandPage;
    }

    @Override
    public boolean saveSysDemand(SysDemandForm sysDemandForm) {
        SysDemand sysDemand = new SysDemand();
        BeanUtil.copyProperties(sysDemandForm, sysDemand);
        sysDemand.setCreateTime(null);
        return this.saveOrUpdate(sysDemand);
    }

    @Override
    public boolean saveSysDemand(SysDemand sysDemand) {
        SysLazyToolUser sysLazyToolUser = sysLazyToolUserMapper.selectById(sysDemand.getUserId());
        sysDemand.setMobile(sysLazyToolUser.getMobile());
        return this.saveOrUpdate(sysDemand);
    }

    @Override
    public SysDemandForm getSysDemandFormData(Long id) {
        SysDemand sysDemand = this.getById(id);
        SysDemandForm sysDemandForm = new SysDemandForm();
        BeanUtil.copyProperties(sysDemand, sysDemandForm);
        return sysDemandForm;
    }

    @Override
    public boolean deleteSysDemand(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysDemandImportVo> voList) {
        List<SysDemand> sysDemandList = new ArrayList<>();
        for (SysDemandImportVo importVo : voList) {
            SysDemand sysDemand = new SysDemand();
            BeanUtil.copyProperties(importVo, sysDemand);
            sysDemandList.add(sysDemand);
        }
        this.saveOrUpdateBatch(sysDemandList);
    }

    @Override
    public List<SysDemandImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysDemand> sysDemandList = this.getBaseMapper().selectBatchIds(ids);
        List<SysDemandImportVo> voList = new ArrayList<>();
        for (SysDemand sysDemand : sysDemandList) {
            SysDemandImportVo vo = new SysDemandImportVo();
            BeanUtil.copyProperties(sysDemand, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public boolean updateResult(SysDemandForm sysDemandForm) {
        SysDemand sysDemand = this.baseMapper.selectById(sysDemandForm.getId());
        // 更新结果
        sysDemand.setResult(sysDemandForm.getResult());
        sysDemand.setStatus(FeeBackEnum.Y.getValue());
        return this.updateById(sysDemand);
    }

    @Override
    public void readDemand(Long demandId) {
        SysDemand sysDemand = this.baseMapper.selectById(demandId);
        // 改成用户已读
        sysDemand.setUserRead(UserReadEnum.Y.getValue());
        this.updateById(sysDemand);
    }

    @Override
    public List<SysDemand> demandList(Long userId, Integer pageNumber) {
        SysDemandQuery queryParams = new SysDemandQuery();
        queryParams.setPageSize(10);
        queryParams.setPageNum(pageNumber);
        queryParams.setUserId(userId);
        Page<SysDemand> demandPage = this.getPage(queryParams);
        return demandPage.getRecords();
    }
}
